<?php
function transferBu($in_user_id,$out_user_id,$amt,$platformNo,$post_url,$deal_id=0,$is_trans = 0,$money_type,$money_memo){
	/* 请求参数 */
		if($in_user_id == $out_user_id){
			return false;
		}
		
		if($in_user_id == 0 ||  $out_user_id == 0){
			return false;
		}
		$in_user = get_user_info("*","id = ".$in_user_id);
		$out_user = get_user_info("*","id = ".$out_user_id);
		
		
		$data = $Sign_data= array();
		$data['in_user_id'] =$in_user_id;
		$data['out_user_id'] =$out_user_id;
		$data['is_trans'] =$is_trans;
		
		if($is_trans == 1)
		{
			$data['transfer_id'] = $deal_id;
			$data['deal_id'] = $GLOBALS['db']->getOne("select deal_id from ".DB_PREFIX."deal_load_transfer where id = ".$deal_id);
		}
		else
		{
			$data['deal_id'] = $deal_id;
		}
		
		$data['in_cust_no'] = $Sign_data['in_cust_no'] = $in_user['mobile'];
		$data['out_cust_no'] = $Sign_data['out_cust_no'] = $out_user['mobile'];
		$data['mchnt_txn_ssn'] = $Sign_data['mchnt_txn_ssn'] = $out_user['id'].get_microtime();//请求流水号
		$data['amount'] = $amt;
		$data['create_time'] = TIME_UTC;
		$data['create_date'] = to_date(TIME_UTC,"Y-m-d");
		
		$data['money_type'] = intval($money_type);
		$data['memo'] = $money_memo;
		
		$Sign_data['mchnt_cd'] = $platformNo;
		$Sign_data['amt'] = round($amt*100,0);//交易金额
		$Sign_data['contract_no'] = '';//合同号 
		$Sign_data['rem'] = '';
		
		$GLOBALS['db']->autoExecute(DB_PREFIX."fuioupay_transferbu",$data,'INSERT');
		
		/* 签名数据 */
		ksort($Sign_data);
		foreach($Sign_data as $k => $v){
			if($k=='rem'){
				$Sign_old.=$v;
			}else{
				$Sign_old.= $v."|";
			}
		}
		
		$pSign=rsaSign($Sign_old,PRIVATE_KEY_PATH);
		
		$req = http_build_query($Sign_data)."&signature=".urlencode($pSign);
		
		$fuioupay_log = array();
		$fuioupay_log['code'] = 'bhaController';
		$fuioupay_log['create_date'] = to_date(TIME_UTC,'Y-m-d H:i:s');
		$fuioupay_log['strxml'] = $req;
		$GLOBALS['db']->autoExecute(DB_PREFIX."fuioupay_log",$fuioupay_log);
		$log_id = $GLOBALS['db']->insert_id();
		
		$options = array(
			CURLOPT_POST => TRUE,
			CURLOPT_RETURNTRANSFER => TRUE,
			CURLOPT_SSL_VERIFYPEER=>0,
			CURLOPT_SSL_VERIFYHOST=>0,
			CURLOPT_POSTFIELDS =>$req,
		
		); 
	    $ch = curl_init($post_url); 
	    curl_setopt_array($ch,$options); 
	   	$resultStr = curl_exec($ch);
	    curl_close($ch); 
		
		$fuioupay_log = array();
		$fuioupay_log['html'] = $resultStr;
		$GLOBALS['db']->autoExecute(DB_PREFIX."fuioupay_log",$fuioupay_log,'UPDATE','id='.$log_id);
		
		if (empty($resultStr)){
			$result = array();
			$result['pErrCode'] = 9999;
			$result['pErrMsg'] = '返回出错';
		}else{
			require_once APP_ROOT_PATH.'system/collocation/fuioupay/xml.php';
			$str3ParaInfo = @XML_unserialize($resultStr);
			$str3Req = $str3ParaInfo['ap']['plain'];
			
			$result = array();
			$result['resp_code'] = $str3Req["resp_code"];
			
			$GLOBALS['db']->query("UPDATE ".DB_PREFIX."fuioupay_transferbu SET is_callback=1,code='".$result['resp_code']."' WHERE is_callback=0 AND mchnt_txn_ssn='".$str3Req["mchnt_txn_ssn"]."'");
			
			$result['mchnt_cd'] = $str3Req["mchnt_cd"];
			$result['mchnt_txn_ssn'] = $str3Req["mchnt_txn_ssn"];
			$result['pErrMsg'] = $str3Req["resp_desc"];
		}
		//$result = json_encode('{"resp_code":"0000","results":{"user_id":"18859145015","ct_balance":"111917","ca_balance":"108017","cf_balance":"3900","cu_balance":"0"},"ct_balance":1119.17,"ca_balance":1080.17,"cf_balance":39,"cu_balance":0}');
		return $result;
}
